package com.ardf.service.impl;

import com.ardf.entity.Athlete;
import com.ardf.entity.AthleteTeam;
import com.ardf.entity.Group;
import com.ardf.entity.dto.AthleteTeamDto;
import com.ardf.mapper.AthleteMapper;
import com.ardf.mapper.AthleteTeamMapper;
import com.ardf.mapper.GroupMapper;
import com.ardf.mapper.TeamMapper;
import com.ardf.service.IAthleteTeamService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@Service
public class AthleteTeamServiceImpl extends ServiceImpl<AthleteTeamMapper, AthleteTeam> implements IAthleteTeamService {

    private final GroupMapper groupMapper;
    private final AthleteMapper athleteMapper;
    private final AthleteTeamMapper athleteTeamMapper;


    public AthleteTeamServiceImpl(GroupMapper groupMapper, AthleteTeamMapper athleteTeamMapper, AthleteMapper athleteMapper) {
        this.groupMapper = groupMapper;
        this.athleteTeamMapper = athleteTeamMapper;
        this.athleteMapper = athleteMapper;
    }

    @Override
    public Group getGroupByTeamId(Long teamId) {
        List<AthleteTeam> athleteTeams = athleteTeamMapper.selectList(new LambdaQueryWrapper<AthleteTeam>()
                .eq(AthleteTeam::getTeamId, teamId));
        if(athleteTeams!=null&&!athleteTeams.isEmpty()){
            Athlete athlete = athleteMapper.selectById(athleteTeams.get(0).getAthleteId());
            return groupMapper.selectById(athlete.getGroupId());
        }
        return null;
    }

    /**
     * author:容伟洪
     * time:2025-3-7
     * description：根据指卡号查找所在的接力队伍
     */
    @Override
    public AthleteTeam selectTeamByAthId(Long athleteId) {
        return athleteTeamMapper.selectOne(new LambdaQueryWrapper<AthleteTeam>().eq(AthleteTeam::getAthleteId,athleteId));
    }

    @Override
    public List<AthleteTeamDto> selectPrintAthleteByTeamId(Long teamId,Long projectId) {
        return athleteTeamMapper.selectPrintAthleteByTeamId(teamId,projectId);
    }
}
